package com.nb.cloud.doc.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nb.cloud.doc.entity.*;
import com.nb.cloud.doc.enums.*;
import com.nb.cloud.doc.mapper.SysKeywordMapper;
import com.nb.cloud.doc.model.vo.SuggestInitVO;
import com.nb.cloud.doc.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 搜索关键词表 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2024-08-04
 */
@Service
public class SysKeywordServiceImpl extends ServiceImpl<SysKeywordMapper, SysKeyword> implements ISysKeywordService {

    @Autowired
    private ISysKeywordService sysKeywordService;

    @Autowired
    private ITextbookChapterService chapterService;
    @Autowired
    private ISysKeywordExtendService extendService;
    //搜索建议的索引
    public static final String suggestIndex = "cloud_doc_suggest";
    //搜索建议名称
    public static final String suggest_name = "keyword_suggest";

    @Autowired
    private ITextbookVersionService textbookVersionService;

    @Autowired
    private IAtlasService atlasService;
    @Autowired
    RestHighLevelClient restHighLevelClient;

    @Autowired
    SysKeywordMapper sysKeywordMapper;

    @Override
    public List<SuggestInitVO> findInitKeywordList() {
        return sysKeywordMapper.selectInitKeywordList();
    }

    @Override
    public void initToES() {
        List<SuggestInitVO> initKeywordList = sysKeywordService.findInitKeywordList();
        for (SuggestInitVO suggestInitVO : initKeywordList) {
            insertSuggestToES(suggestInitVO);
        }

    }

    @Override
    public void intToDB() {
        versionGradeTerm();
        versionGradeTermSubject();
        versionGradeSubject();
        versionGradeSubjectTerm();
        versionGradeSubjectTerm();
        versionSubjectGradeTerm();
        gradeTermSubjectVersion();
        gradeSubjectTerm();
        gradeTermSubject();
        subjectGradeTermVersion();
        chapterToKeyword();
        handlerAtlas();
    }

    //教材版本+年级+学期
    private void versionGradeTerm() {
        List<TextbookVersion> list = textbookVersionService.list();

        for (TextbookVersion version : list) {
            for (GradeEnum gradeEnum : GradeEnum.values()) {
                for (TermEnum termEnum : TermEnum.values()) {
                    String kw = StringUtils.join(version.getName(), gradeEnum.getText(), termEnum.getText());
                    String kwType = StringUtils.join(RulesEnum.VERSION.getDesc(),
                            "/", RulesEnum.GRADE.getDesc(),
                            "/", RulesEnum.TERM.getDesc());
                    String rules = StringUtils.join(
                            RulesEnum.VERSION.getBizId(), "=", version.getTextbookVersionId(),
                            "&", RulesEnum.GRADE.getBizId(), "=", gradeEnum.getCode(),
                            "&", RulesEnum.TERM.getBizId(), "=", termEnum.getCode());

                    String bizId = StringUtils.join(
                            version.getTextbookVersionId(),
                            "/", gradeEnum.getCode(),
                            "/", termEnum.getCode());

                    SysKeyword kwEntity = SysKeyword.builder()
                            .sysKeywordName(kw)
                            .sysKeywordType(kwType)
                            .rules(rules)
                            .bizId(bizId)
                            .build();
                    sysKeywordService.save(kwEntity);
                    //别名
                    SysKeywordExtend extend = SysKeywordExtend.builder()
                            .sysKeywordId(kwEntity.getId())
                            .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                            .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                            .build();
                    extendService.save(extend);
                }
            }

        }
    }

    //教材版本+年级+学期+学科
    private void versionGradeTermSubject() {
        List<TextbookVersion> list = textbookVersionService.list();

        for (TextbookVersion version : list) {
            for (GradeEnum gradeEnum : GradeEnum.values()) {
                for (TermEnum termEnum : TermEnum.values()) {
                    for (SubjectEnum subjectEnum : SubjectEnum.values()) {
                        String kw = StringUtils.join(version.getName(), gradeEnum.getText(), termEnum.getText());
                        String kwType = StringUtils.join(
                                RulesEnum.VERSION.getDesc(),
                                "/", RulesEnum.GRADE.getDesc(),
                                "/", RulesEnum.TERM.getDesc(),
                                "/", RulesEnum.SUBJECT.getDesc());
                        String rules = StringUtils.join(
                                RulesEnum.VERSION.getBizId(), "=", version.getTextbookVersionId(),
                                "&", RulesEnum.GRADE.getBizId(), "=", gradeEnum.getCode(),
                                "&", RulesEnum.TERM.getBizId(), "=", termEnum.getCode(),
                                "&", RulesEnum.SUBJECT.getBizId(), "=", subjectEnum.getCode());

                        String bizId = StringUtils.join(
                                version.getTextbookVersionId(),
                                "/", gradeEnum.getCode(),
                                "/", termEnum.getCode(),
                                "/", subjectEnum.getCode());

                        SysKeyword kwEntity = SysKeyword.builder()
                                .sysKeywordName(kw)
                                .sysKeywordType(kwType)
                                .rules(rules)
                                .bizId(bizId)
                                .build();
                        sysKeywordService.save(kwEntity);
                        //别名
                        SysKeywordExtend extend = SysKeywordExtend.builder()
                                .sysKeywordId(kwEntity.getId())
                                .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                                .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                                .build();
                        extendService.save(extend);

                        kw = StringUtils.join(kw, subjectEnum.getText());
                        kwType = StringUtils.join(kwType, "/", subjectEnum.getText());
                        rules = StringUtils.join(rules,
                                "&", RulesEnum.SUBJECT.getBizId(), "=", subjectEnum.getCode());
                        bizId = StringUtils.join(bizId, "/", subjectEnum.getCode());

                        kwEntity.setId(null);
                        kwEntity.setSysKeywordName(kw);
                        kwEntity.setSysKeywordType(kwType);
                        kwEntity.setRules(rules);
                        kwEntity.setBizId(bizId);
                        sysKeywordService.save(kwEntity);
                        extend.setId(null);
                        extend.setSysKeywordId(kwEntity.getId());
                        extendService.save(extend);
                    }


                }
            }

        }
    }


    //教材版本+年级+学科
    private void versionGradeSubject() {
        List<TextbookVersion> list = textbookVersionService.list();

        for (TextbookVersion version : list) {
            for (GradeEnum gradeEnum : GradeEnum.values()) {
                for (SubjectEnum subjectEnum : SubjectEnum.values()) {
                    String kw = StringUtils.join(version.getName(), gradeEnum.getText(), subjectEnum.getText());
                    String kwType = StringUtils.join(
                            RulesEnum.VERSION.getDesc(),
                            "/", RulesEnum.GRADE.getDesc(),
                            "/", RulesEnum.SUBJECT.getDesc());
                    String rules = StringUtils.join(
                            RulesEnum.VERSION.getBizId(), "=", version.getTextbookVersionId(),
                            "&", RulesEnum.GRADE.getBizId(), "=", gradeEnum.getCode(),
                            "&", RulesEnum.SUBJECT.getBizId(), "=", subjectEnum.getCode());

                    String bizId = StringUtils.join(
                            version.getTextbookVersionId(),
                            "/", gradeEnum.getCode(),
                            "/", subjectEnum.getCode());

                    SysKeyword kwEntity = SysKeyword.builder()
                            .sysKeywordName(kw)
                            .sysKeywordType(kwType)
                            .rules(rules)
                            .bizId(bizId)
                            .build();
                    sysKeywordService.save(kwEntity);
                    //别名
                    SysKeywordExtend extend = SysKeywordExtend.builder()
                            .sysKeywordId(kwEntity.getId())
                            .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                            .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                            .build();
                    extendService.save(extend);
                }
            }

        }
    }

    //教材版本+年级+学科+学期
    private void versionGradeSubjectTerm() {
        List<TextbookVersion> list = textbookVersionService.list();

        for (TextbookVersion version : list) {
            for (GradeEnum gradeEnum : GradeEnum.values()) {
                for (SubjectEnum subjectEnum : SubjectEnum.values()) {
                    for (TermEnum termEnum : TermEnum.values()) {
                        String kw = StringUtils.join(version.getName(), gradeEnum.getText(), subjectEnum.getText(), termEnum.getText());
                        String kwType = StringUtils.join(
                                RulesEnum.VERSION.getDesc(),
                                "/",RulesEnum.GRADE.getDesc(),
                                "/", RulesEnum.SUBJECT.getDesc(),
                                "/", RulesEnum.TERM.getDesc());
                        String rules = StringUtils.join(
                                RulesEnum.VERSION.getBizId(), "=", version.getTextbookVersionId(),
                                "&", RulesEnum.GRADE.getBizId(), "=", gradeEnum.getCode(),
                                "&", RulesEnum.SUBJECT.getBizId(), "=", subjectEnum.getCode(),
                                "&", RulesEnum.TERM.getBizId(), "=", termEnum.getCode());

                        String bizId = StringUtils.join(
                                version.getTextbookVersionId(),
                                "/", gradeEnum.getCode(),
                                "/", subjectEnum.getCode(),
                                "/", termEnum.getCode());

                        SysKeyword kwEntity = SysKeyword.builder()
                                .sysKeywordName(kw)
                                .sysKeywordType(kwType)
                                .rules(rules)
                                .bizId(bizId)
                                .build();
                        sysKeywordService.save(kwEntity);
                        //别名
                        SysKeywordExtend extend = SysKeywordExtend.builder()
                                .sysKeywordId(kwEntity.getId())
                                .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                                .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                                .build();
                        extendService.save(extend);
                    }

                }
            }

        }
    }


    //教材版本+学科+年级+学期
    private void versionSubjectGradeTerm() {
        List<TextbookVersion> list = textbookVersionService.list();
        for (TextbookVersion version : list) {
            for (SubjectEnum subjectEnum : SubjectEnum.values()) {
                for (GradeEnum gradeEnum : GradeEnum.values()) {
                    for (TermEnum termEnum : TermEnum.values()) {
                        String kw = StringUtils.join(
                                version.getName(),
                                subjectEnum.getText(),
                                gradeEnum.getText(),
                                termEnum.getText());
                        String kwType = StringUtils.join(
                                RulesEnum.VERSION.getDesc(),
                                "/", RulesEnum.SUBJECT.getDesc(),
                                "/", RulesEnum.GRADE.getDesc(),
                                "/", RulesEnum.TERM.getDesc());
                        String rules = StringUtils.join(
                                RulesEnum.VERSION.getBizId(), "=", version.getTextbookVersionId(),
                                "&", RulesEnum.SUBJECT.getBizId(), "=", subjectEnum.getCode(),
                                "&", RulesEnum.GRADE.getBizId(), "=", gradeEnum.getCode(),
                                "&", RulesEnum.TERM.getBizId(), "=", termEnum.getCode());

                        String bizId = StringUtils.join(
                                version.getTextbookVersionId(),
                                "/", subjectEnum.getCode(),
                                "/", gradeEnum.getCode(),
                                "/", termEnum.getCode());

                        SysKeyword kwEntity = SysKeyword.builder()
                                .sysKeywordName(kw)
                                .sysKeywordType(kwType)
                                .rules(rules)
                                .bizId(bizId)
                                .build();
                        sysKeywordService.save(kwEntity);
                        //别名
                        SysKeywordExtend extend = SysKeywordExtend.builder()
                                .sysKeywordId(kwEntity.getId())
                                .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                                .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                                .build();
                        extendService.save(extend);
                    }

                }
            }

        }
    }

    //年级+学期+学科
    private void gradeTermSubject() {
        for (GradeEnum gradeEnum : GradeEnum.values()) {
            for (TermEnum termEnum : TermEnum.values()) {
                for (SubjectEnum subjectEnum : SubjectEnum.values()) {
                    String kw = StringUtils.join(gradeEnum.getText(), termEnum.getText(), subjectEnum.getText());
                    String kwType = StringUtils.join(
                            RulesEnum.GRADE.getDesc(),
                            "/", RulesEnum.TERM.getDesc(),
                            "/", RulesEnum.SUBJECT.getDesc());
                    String rules = StringUtils.join(
                            RulesEnum.GRADE.getBizId(), "=", gradeEnum.getCode(),
                            "&", RulesEnum.TERM.getBizId(), "=", termEnum.getCode(),
                            "&", RulesEnum.SUBJECT.getBizId(), "=", subjectEnum.getCode()
                    );
                    String bizId = StringUtils.join(
                            gradeEnum.getCode(),
                            "/", termEnum.getCode(),
                            "/", subjectEnum.getCode());
                    SysKeyword kwEntity = SysKeyword.builder()
                            .sysKeywordName(kw)
                            .sysKeywordType(kwType)
                            .rules(rules)
                            .bizId(bizId)
                            .build();
                    sysKeywordService.save(kwEntity);
                    //别名
                    SysKeywordExtend extend = SysKeywordExtend.builder()
                            .sysKeywordId(kwEntity.getId())
                            .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                            .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                            .build();
                    extendService.save(extend);
                }
            }
        }
    }


    //年级+学科 学期
    private void gradeSubjectTerm() {
        for (GradeEnum gradeEnum : GradeEnum.values()) {
            for (SubjectEnum subjectEnum : SubjectEnum.values()) {
            for (TermEnum termEnum : TermEnum.values()) {
                    String kw = StringUtils.join(gradeEnum.getText(), subjectEnum.getText(), termEnum.getText());
                    String kwType = StringUtils.join(RulesEnum.GRADE.getDesc(),
                            "/", RulesEnum.SUBJECT.getDesc(),
                            "/", RulesEnum.TERM.getDesc()
                          );
                    String rules = StringUtils.join(
                            RulesEnum.GRADE.getBizId(), "=", gradeEnum.getCode(),
                            "&", RulesEnum.SUBJECT.getBizId(), "=", subjectEnum.getCode(),
                            "&", RulesEnum.TERM.getBizId(), "=", termEnum.getCode()

                    );
                    String bizId = StringUtils.join(
                            gradeEnum.getCode(),
                            "/", subjectEnum.getCode(),
                            "/", termEnum.getCode());
                    SysKeyword kwEntity = SysKeyword.builder()
                            .sysKeywordName(kw)
                            .sysKeywordType(kwType)
                            .rules(rules)
                            .bizId(bizId)
                            .build();
                    sysKeywordService.save(kwEntity);
                    //别名
                    SysKeywordExtend extend = SysKeywordExtend.builder()
                            .sysKeywordId(kwEntity.getId())
                            .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                            .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                            .build();
                    extendService.save(extend);
                }
            }
        }
    }


    //年级+学期+学科+教材版本
    private void gradeTermSubjectVersion() {
        List<TextbookVersion> versionList = textbookVersionService.list();
        for (GradeEnum gradeEnum : GradeEnum.values()) {
            for (TermEnum termEnum : TermEnum.values()) {
                for (SubjectEnum subjectEnum : SubjectEnum.values()) {
                    for (TextbookVersion version : versionList) {
                        String kw = StringUtils.join(
                                gradeEnum.getText(),
                                termEnum.getText(),
                                subjectEnum.getText(),
                                version.getName());
                        String kwType = StringUtils.join(RulesEnum.GRADE.getDesc(),
                                "/", RulesEnum.TERM.getDesc(),
                                "/",RulesEnum.SUBJECT.getDesc(),
                                "/", RulesEnum.VERSION.getDesc());
                        String rules = StringUtils.join(
                                RulesEnum.GRADE.getBizId(), "=", gradeEnum.getCode(),
                                "&", RulesEnum.TERM.getBizId(), "=", termEnum.getCode(),
                                "&", RulesEnum.SUBJECT.getBizId(), "=", subjectEnum.getCode(),
                                "&", RulesEnum.VERSION.getBizId(), "=", version.getTextbookVersionId()
                        );
                        String bizId = StringUtils.join(
                                gradeEnum.getCode(),
                                "/", termEnum.getCode(),
                                "/", subjectEnum.getCode(),
                                "/", version.getTextbookVersionId());
                        SysKeyword kwEntity = SysKeyword.builder()
                                .sysKeywordName(kw)
                                .sysKeywordType(kwType)
                                .rules(rules)
                                .bizId(bizId)
                                .build();
                        sysKeywordService.save(kwEntity);
                        //别名
                        SysKeywordExtend extend = SysKeywordExtend.builder()
                                .sysKeywordId(kwEntity.getId())
                                .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                                .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                                .build();
                        extendService.save(extend);
                    }

                }
            }
        }
    }


    //学科+年级+学期+教材版本
    private void subjectGradeTermVersion() {
        List<TextbookVersion> versionList = textbookVersionService.list();
        for (SubjectEnum subjectEnum : SubjectEnum.values()) {
            for (GradeEnum gradeEnum : GradeEnum.values()) {
                for (TermEnum termEnum : TermEnum.values()) {
                    for (TextbookVersion version : versionList) {
                        String kw = StringUtils.join( subjectEnum.getText(),
                                gradeEnum.getText(),
                                termEnum.getText(),
                                version.getName()
                        );
                        String kwType = StringUtils.join(
                                RulesEnum.SUBJECT.getDesc(),
                                "/", RulesEnum.GRADE.getDesc(),
                                "/", RulesEnum.TERM.getDesc(),
                                "/", RulesEnum.VERSION.getDesc()
                                );
                        String rules = StringUtils.join(
                                RulesEnum.SUBJECT.getBizId(), "=", subjectEnum.getCode(),
                                "&",  RulesEnum.GRADE.getBizId(), "=", gradeEnum.getCode(),
                                "&", RulesEnum.TERM.getBizId(), "=", termEnum.getCode(),
                                "&", RulesEnum.VERSION.getBizId(), "=", version.getTextbookVersionId()
                        );
                        String bizId = StringUtils.join(
                                subjectEnum.getCode(),
                                "/", gradeEnum.getCode(),
                                "/", termEnum.getCode(),
                                "/", version.getTextbookVersionId());
                        SysKeyword kwEntity = SysKeyword.builder()
                                .sysKeywordName(kw)
                                .sysKeywordType(kwType)
                                .rules(rules)
                                .bizId(bizId)
                                .build();
                        sysKeywordService.save(kwEntity);
                        //别名
                        SysKeywordExtend extend = SysKeywordExtend.builder()
                                .sysKeywordId(kwEntity.getId())
                                .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                                .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                                .build();
                        extendService.save(extend);
                    }

                }
            }
        }
    }



    private void chapterToKeyword(){
        List<TextbookChapter> list = chapterService.list();
        for(TextbookChapter chapter : list){
            //这里需要用到扩展词：
            //~============数学：============
            //关键词：乘法  扩展词：0的乘法
            //关键词：乘法  扩展词：0的乘法
            // 1、数学符号处理 关键词：大于小于等于  扩展词
            handlerMathChapter(chapter);
            handlerChineseChapter(chapter);
            handlerEnglishChapter(chapter);
        }
    }

    /**
     * 处理数学章节
     * @param chapter
     */
    private void handlerMathChapter(TextbookChapter chapter){
        if(chapter.getSubjectCode() != 2){
           return;
        }
        if(StringUtils.equals(chapter.getParentId(),"-1")){
            System.out.println("数学章节不处理单元");
            return;
        }
        //特殊符号去除：”
        //>=<  ----> 大于小于等于
        //关键词处理：比如，人民币 ----> 认识1元以上的人民币

        //乘法 ---> 0的乘法

        // 大小比较 比较大小 ---> 100以内数的大小比较

        //排序 -->  20以内数的大小与排序

        //乘法口诀  ----> 4的乘法口诀 5的乘法口诀
        String kw = StringUtils.replace(chapter.getName(),">","大于")
                .replace("=","等于")
                .replace("<","小于")
                .replace("”","");

        String rules = RulesEnum.CHAPTER.getBizId()+"="+chapter.getChapterId();

        SysKeyword kwEntity = SysKeyword.builder()
                .sysKeywordName(kw)
                .sysKeywordType(KeywordTypeEnum.CHAPTER.name())
                .rules(rules)
                .bizId(chapter.getChapterId())
                .build();
        sysKeywordService.save(kwEntity);
        //别名
        SysKeywordExtend extend = SysKeywordExtend.builder()
                .sysKeywordId(kwEntity.getId())
                .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                .build();
        extendService.save(extend);
    }


    /**
     * 处理语文章节
     * @param chapter
     */
    private void handlerChineseChapter(TextbookChapter chapter){
        if(chapter.getSubjectCode() != 1){
            return;
        }
        if(StringUtils.equals(chapter.getParentId(),"-1")){
            System.out.println("语文章节不处理单元");
            return;
        }
        //~============语文：============
        //特殊符号去除 “《》【】 空格去除 -
        //ɑ --> a

        String kw = StringUtils.replace(chapter.getName(),"“","")
                .replace("《","")
                .replace("》","")
                .replace("【","")
                .replace("】","")
                .replace(" ","")
                .replace("-","")
                .replace("ɑ","a");

        String rules = RulesEnum.CHAPTER.getBizId()+"="+chapter.getChapterId();

        SysKeyword kwEntity = SysKeyword.builder()
                .sysKeywordName(kw)
                .sysKeywordType(KeywordTypeEnum.CHAPTER.name())
                .rules(rules)
                .bizId(chapter.getChapterId())
                .build();
        sysKeywordService.save(kwEntity);
        //别名
        SysKeywordExtend extend = SysKeywordExtend.builder()
                .sysKeywordId(kwEntity.getId())
                .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                .build();
        extendService.save(extend);
    }


    /**
     * 处理英语章节
     * @param chapter
     */
    private void handlerEnglishChapter(TextbookChapter chapter){
        if(chapter.getSubjectCode() != 3){
            return;
        }
        if(!StringUtils.equals(chapter.getParentId(),"-1")){
            System.out.println("英语只处理单元名字 parentId=-1");
            return;
        }
        String kw = StringUtils.replace(chapter.getName(),"?","");

        //上册-第一单元 School  ---> keyword全转小写 school  extend扩展词： School
        //第八单元 My dad is a doctor
        //通过 单元 分割，取后面的

        String split = "单元 ";
        String sourceName = StringUtils.substringAfter(kw, split);

        String rules = RulesEnum.CHAPTER.getBizId()+"="+chapter.getChapterId();

        SysKeyword kwEntity = SysKeyword.builder()
                .sysKeywordName(sourceName.toLowerCase())
                .sysKeywordType(KeywordTypeEnum.CHAPTER.name())
                .rules(rules)
                .bizId(chapter.getChapterId())
                .build();
        sysKeywordService.save(kwEntity);

        //别名
        SysKeywordExtend extend = SysKeywordExtend.builder()
                .sysKeywordId(kwEntity.getId())
                .extKeywordName(PinyinUtil.getPinyin(kw, ""))
                .extKeywordType(ExtKeywordTypeEnum.FULL_PINYIN.getType())
                .build();
        extendService.save(extend);

    }



    /**
     * 处理知识点
     * @param
     */
    private void handlerAtlas(){
        List<Atlas> list = atlasService.list();
        for(Atlas atlas : list){
            String kw = StringUtils.replace(atlas.getName(),"“","")
                    .replace("《","")
                    .replace("》","")
                    .replace("【","")
                    .replace("】","")
                    .replace(" ","")
                    .replace("-","")
                    .replace("ɑ","a");

            //上册-第一单元 School  ---> keyword全转小写 school  extend扩展词： School
            //第八单元 My dad is a doctor
            //通过 单元 分割，取后面的

            String rules = RulesEnum.ATLAS.getBizId()+"="+atlas.getAtlasId();

            SysKeyword kwEntity = SysKeyword.builder()
                    .sysKeywordName(kw.toLowerCase())
                    .sysKeywordType(KeywordTypeEnum.ATLAS.name())
                    .rules(rules)
                    .bizId(atlas.getAtlasId())
                    .build();
            sysKeywordService.save(kwEntity);
        }
    }

    private void insertSuggestToES(SuggestInitVO suggestInitVO) {
        try {
// 新增文档 - 请求对象
            IndexRequest request = new IndexRequest();
            // 设置索引及唯一性标识
            String id = IdUtil.simpleUUID();
            suggestInitVO.setId(id);
            request.index(suggestIndex).id(id);

            ObjectMapper objectMapper = new ObjectMapper();
            String suggestJson = objectMapper.writeValueAsString(suggestInitVO);
            // 添加文档数据，数据格式为 JSON 格式
            request.source(suggestJson, XContentType.JSON);
            // 客户端发送请求，获取响应对象
            IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
            ////3.打印结果信息
            System.out.println("_index:" + response.getIndex());
            System.out.println("_id:" + response.getId());
            System.out.println("_result:" + response.getResult());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
